
package io.renren.modules.jxgkphone.controller;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.entity.StudentInfoEntity;
import io.renren.modules.jxgk.entity.TempStudentAttendenceEntity;
import io.renren.modules.jxgkphone.dao.ClassCourseMapper;
import io.renren.modules.jxgkphone.entity.ClassCourse;
import io.renren.modules.jxgkphone.service.ClassAttendanceService;
import io.renren.modules.jxgkphone.tempEntity.TempAttend;
import io.renren.modules.jxgkphone.tempEntity.TempStudentAttendence;
import io.renren.modules.jxgkphone.tempEntity.TempUpdateAttend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wuwenze.poi.ExcelKit;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hello")
public class ClassAttendanceController {

    @Autowired
    ClassAttendanceService classAttendanceService;
    @Autowired
    ClassCourseMapper classCourseMapper;


    //第一个是返回班级同学出勤情况
    @RequestMapping("/{cid}/{scid}/FindClassAttendence")
    public Map  FindClassAttendence(@PathVariable("cid") String cid, @PathVariable("scid") String scid){
        List<TempAttend> stuAD=classAttendanceService.FindClassAttendence(cid,scid);
        ClassCourse classCourse=classCourseMapper.getCourseById(cid);
        Map map=new HashMap();
        //返回出勤状态的同时还要返回班委操作考勤的次数
        map.put("stuAD",stuAD);
        map.put("isAttendance",classCourse.getIsAttendance());
        return map;
    }





    //第二个：查询->更新（插入）->查询
    @RequestMapping("/UpdateStatus/{token}/{id}")
    public List<TempAttend> UpdateStatus(@RequestBody TempUpdateAttend tempUpdateAttend){
        List<TempAttend> stuAD=classAttendanceService.UpdateStatus(tempUpdateAttend);
        return stuAD;
    }
    @RequestMapping("/selectStudentAttendence")
    public R selectStudentAttendence(@RequestParam Map<String,Object> map){
        Query query=new Query(map);
        List<TempStudentAttendence> cse=classAttendanceService.selectStudentAttendence( query);
        int total = classAttendanceService.queryTotal( query);
        PageUtils pageUtil = new PageUtils(cse, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
    @RequestMapping("/export")
    public R export(HttpServletResponse response,String className) {
        List<TempStudentAttendence> cse=null;
        Map<String,Object> map=new HashMap<String,Object>();
        if(className!=null&&!className.equals("")){
            map.put("className",className);
        }
        try{
            cse=classAttendanceService.selectStudentAttendence( map);
            ExcelKit.$Export(TempStudentAttendence.class, response).toExcel(cse, "学生考勤汇总");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return R.ok();
    }



}

